<?php

namespace app\models;

use Yii;
use yii\db\Expression;
/**
 * This is the model class for table "charge_cash_deal".
 *
 * @property integer $id
 * @property integer $type
 * @property integer $channel
 * @property string $realname
 * @property string $reg_dt
 * @property string $orderno
 * @property string $orderlink
 * @property integer $member_id
 * @property double $money
 * @property integer $status
 * @property string $approve_dt
 *
 * @property Member $member
 * @property NeaAmountChange[] $neaAmountChanges
 * @property UsdAmountChange[] $usdAmountChanges
 */
class ChargeCashDeal extends \yii\db\ActiveRecord
{
  const TYPE_CHARGE=1;
  const TYPE_CASH=2;
  const TYPE_USD_NEA=3;
  const TYPE_NEA_USD=4;

  const CHANNEL_SHOUXINYI=1;//首信易
  const CHANNEL_ALIPAY=2;//支付宝
  const CHANNEL_WEIXIN=3;//微信支付

  const STATUS_REGISTER=0;//支付宝充值申请实际扫描成功后提请审核
  const STATUS_PROVED=1;//支付宝充值申请审批用户实际扫描成功后提请审核
  const STATUS_UNPROVED=2;
  const STATUS_DISCARD=3;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'charge_cash_deal';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type', 'channel', 'member_id', 'money', 'status'], 'required'],
            [['type', 'channel', 'member_id', 'status'], 'integer'],
            [['reg_dt', 'approve_dt'], 'safe'],
            [['money'], 'number'],
            [['realname'], 'string', 'max' => 30],
            [['orderno'], 'string', 'max' => 25],
            [['orderlink'], 'string', 'max' => 256],
            [['member_id'], 'exist', 'skipOnError' => true, 'targetClass' => Member::className(), 'targetAttribute' => ['member_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
          'id' => 'ID',
          'type' => '操作类型',
          'channel' => '通道',
          'realname' => '真实姓名',
          'reg_dt' => '申请日期',
          'orderno' => '交易单号',
          'orderlink' => '订单链接',//对于支付宝为上传的订单图片地址
          'member_id' => '会员ID',
          'money' => '提现/充值/转账金额',
          'status' => '状态',
          'approve_dt' => '审核日期',
        ];
    }
    public function attributeValues()
    {
      return [
        'type'=>[
          '1'=>'充值',
          '2'=>'提现',
          '3'=>'从USD账户转NEA账户',
          '4'=>'从NEA账户转USD账户',
        ],
        'status' => [
          '0'=>'申请',
          '1'=>'申请审批通过',
          '2'=>'申请审批不通过',  //失败1
          '3'=>'作废',           //作废
          '4'=>'支付宝转账用户确认', //成功1
          '5'=>'支付宝转账管理员确认',//成功2
          '6'=>'支付宝转账管理员否定',//失败2
        ],
        'channel' => [
          '1'=>'通道一（网银）',
          '2'=>'通道二（支付宝）',
          '3'=>'通道三（微信支付）',
        ],

      ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMember()
    {
        return $this->hasOne(Member::className(), ['id' => 'member_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getNeaAmountChanges()
    {
        return $this->hasMany(NeaAmountChange::className(), ['deal_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUsdAmountChanges()
    {
        return $this->hasMany(UsdAmountChange::className(), ['deal_id' => 'id']);
    }
    /**
     * 支付宝充值申请
     */
    /*public static function chargeRegisterForAlipay($model){
      $charge = new ChargeCashDeal();
      $charge->channel=ChargeCashDeal::CHANNEL_ALIPAY;
      $charge->type=ChargeCashDeal::TYPE_CHARGE;
      $charge->realname='';//$model->real_name;//充值人
      $charge->reg_dt=new Expression('NOW()');
      $charge->member_id= Yii::$app->user->getId();//操作人
      $charge->money=$model->money;
      $charge->status=ChargeCashDeal::STATUS_REGISTER;
      $charge->orderno=$model->orderno;
      $charge->orderlink=$model->orderlink;
      if(!$charge->save()){
        return false;
      }
      return $charge;
    }*/

    /**
     * 支付宝充值申请(在进行账户实际划拨时进行汇率转换)
     */
    public static function chargeRegisterForAlipay($model){
      $charge = new ChargeCashDeal();
      $charge->channel=ChargeCashDeal::CHANNEL_ALIPAY;
      $charge->type=ChargeCashDeal::TYPE_CHARGE;
      $charge->realname='';//$model->real_name;//充值人
      $charge->reg_dt=new Expression('NOW()');
      $charge->member_id= Yii::$app->user->getId();//操作人
      $charge->money=$model->money;
      $charge->status=ChargeCashDeal::STATUS_REGISTER;
      $charge->orderno=$model->orderno;
      $charge->orderlink=$model->orderlink;
      if(!$charge->save()){
        Yii::getLogger()->log(print_r($charge->errors,true),yii\log\Logger::LEVEL_ERROR,'MyLog');
        return false;
      }
      return $charge->id;
    }

    /**
     * 查询未审核的支付宝充值登记
     */
    public static function queryChargeRegForAlipayUnfinished($userid){
      $model = ChargeCashDeal::find()->where([
        'member_id'=>$userid,
        'channel'=>ChargeCashDeal::CHANNEL_ALIPAY,
        'type'=>ChargeCashDeal::TYPE_CHARGE,
        'status'=>ChargeCashDeal::STATUS_REGISTER
        ])->One();
      if(is_null($model)){
        return false;
      }
      return $model;
    }

    /**
     * 支付宝充值审核
     */
    public static function alipayCongzhiProve($id,$status){
      $model = ChargeCashDeal::findOne($id);
      if(is_null($model)){
        return false;
      }
      $model->status=$status;
      if(!$model->save()){
        return false;
      }
      return true;
    }



    /**
     * 网银充值申请(在进行账户实际划拨时进行汇率转换)
     */
    public static function chargeRegisterForPayease($orderno,$money){
      $charge = new ChargeCashDeal();
      $charge->channel=ChargeCashDeal::CHANNEL_SHOUXINYI;
      $charge->type=ChargeCashDeal::TYPE_CHARGE;
      $charge->realname='';//$model->real_name;//充值人
      $charge->reg_dt=new Expression('NOW()');
      $charge->member_id= Yii::$app->user->getId();//操作人
      $charge->money=$money;
      $charge->status=ChargeCashDeal::STATUS_DISCARD;
      $charge->orderno=$orderno;
      $charge->orderlink='';
      if(!$charge->save()){
        Yii::getLogger()->log(print_r($charge->errors,true),yii\log\Logger::LEVEL_ERROR,'MyLog');
        return false;
      }
      return $charge->id;
    }

    /**
     * 网银充值结果确认
     */
    public static function payeaseCongzhiProve($oid,$status){
      $model = ChargeCashDeal::find()
      ->where([
        'orderno'=>$oid
        ])->One();
      if(is_null($model)){
        return false;
      }
      $model->status=$status;
      if(!$model->save()){

        return false;
      }
      return true;
    }

    /*
    * 微信扫码支付登记
    */
    public static function chargeRegisterForWeixin($model){
      $charge = new ChargeCashDeal();
      $charge->channel=ChargeCashDeal::CHANNEL_WEIXIN;
      $charge->type=ChargeCashDeal::TYPE_CHARGE;
      $charge->realname='';//$model->real_name;//充值人
      $charge->reg_dt=new Expression('NOW()');
      $charge->member_id= Yii::$app->user->getId();//操作人
      $charge->money=$model->money;
      $charge->status=ChargeCashDeal::STATUS_DISCARD;
      $charge->orderno=$model->orderno;
      $charge->orderlink=$model->orderlink;
      if(!$charge->save()){
        Yii::getLogger()->log(print_r($charge->errors,true),yii\log\Logger::LEVEL_ERROR,'MyLog');
        return false;
      }
      return $charge->id;
    }

    /**
     * 微信充值结果确认
     */
    public static function weixinCongzhiProve($oid,$status){
      $model = ChargeCashDeal::find()
      ->where([
        'orderno'=>$oid
        ])->One();
      if(is_null($model)){
        return false;
      }
      $model->status=$status;
      if(!$model->save()){

        return false;
      }
      return true;
    }

    /**
     * 提现审核
     */
    public static function tixianProve($id,$status){
      $model = ChargeCashDeal::findOne($id);
      if(is_null($model)){
        return false;
      }
      $model->status=$status;
      if(!$model->save()){
        return false;
      }
      return true;
    }
    /**
     * 统计提现次数
     */
    public static function totalTixianCount($userid){
      return ChargeCashDeal::find()->where([
        'member_id'=>$userid,
        'type'=>ChargeCashDeal::TYPE_CASH,
        'status'=>ChargeCashDeal::STATUS_PROVED
        ])->count();
    }

    /**
     * 是否还有未审核的提现申请
     */
    public static function haveTixianRegUnfinished($userid){
      //ASSERT $userid==Yii::$app->user->getUserId()
      $have = ChargeCashDeal::find()->where([
        'member_id'=>$userid,
        'type'=>ChargeCashDeal::TYPE_CASH,
        'status'=>ChargeCashDeal::STATUS_REGISTER
        ])->count();
      if($have>0){
        return true;
      }
      return false;
    }


    /**
     * 提现申请
     */
    public static function tixianReg($userid,$realname,$money){
      //ASSERT $userid==Yii::$app->user->getUserId()
      $model=new ChargeCashDeal;
      $model->member_id = $userid;
      $model->realname = $realname;
      $model->money=$money;
      $model->type=ChargeCashDeal::TYPE_CASH;
      $model->status=ChargeCashDeal::STATUS_REGISTER;
      $model->reg_dt=new Expression('NOW()');
      $model->channel=ChargeCashDeal::CHANNEL_ALIPAY;//不关心
      $model->orderno='0000000000';
      $model->orderlink='';
      if(!$model->save()){
        return false;
      }
      return true;
    }

    /**
     * 资金互转（无需审核）
     */
    public static function zijinHuzhuan($type,$money){
      //ASSERT $userid==Yii::$app->user->getUserId()
      $model=new ChargeCashDeal;
      $model->member_id = Yii::$app->user->getId();
      $model->realname = '';
      $model->money=$money;
      $model->type=$type;//TYPE_USD_NEA=3;TYPE_NEA_USD=4;
      $model->status=ChargeCashDeal::STATUS_PROVED;//无需审核一步到位
      $model->reg_dt=new Expression('NOW()');
      $model->channel=ChargeCashDeal::CHANNEL_ALIPAY;//不关心
      $model->orderno='0000000000';
      $model->orderlink='';
      if(!$model->save()){
        return false;
      }
      return true;
    }
    /**
     * 资金互转审核
     */
    /*public static function zijinhuzhuanProve($id,$status){
      $model = ChargeCashDeal::findOne($id);
      if(is_null($model)){
        return false;
      }
      $model->status=$status;
      if(!$model->save()){
        return false;
      }
      return true;
    }*/

}
